import androidx.room.gradle.RoomExtension
import com.google.devtools.ksp.gradle.KspExtension
import com.yu.hu.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies

/**
 * Android Library插件
 *
 * 引入Room相关依赖
 *
 * huyu create
 * 2025/10/22 9:19
 */
@Suppress("unused")
class AndroidRoomConventionPlugin : Plugin<Project> {
    /**
     * 插件应用入口
     *
     * @param target 目标项目实例
     */
    override fun apply(target: Project) {
        with(target) {
            with(pluginManager) {
                apply("androidx.room")
                apply("com.google.devtools.ksp")
            }

            extensions.configure<KspExtension> {
                //生成kotlin源码
                arg("room.generateKotlin", "true")
            }

            extensions.configure<RoomExtension> {
                schemaDirectory("$projectDir/schemas")
                println("配置room.schemaLocation:${projectDir}/schemas")
            }

            dependencies {
                "implementation"(libs.findLibrary("androidx.room.runtime").get())
                // optional - Kotlin Extensions and Coroutines support for Room
                "implementation"(libs.findLibrary("androidx.room.ktx").get())
                // optional - Paging 3 Integration
                "implementation"(libs.findLibrary("androidx.room.paging").get())

                "testImplementation"(libs.findLibrary("androidx.room.testing").get())

                "ksp"(libs.findLibrary("androidx.room.compiler").get())
            }
        }
    }
}